#include "inwidget.h"

InWidget::InWidget(QWidget *parent) : QWidget(parent)
{
    this->init_Widget();
    this->init_Control();
    this->init_Connect();
}

void InWidget::init_Control()
{
    this->returnBtn=new QPushButton();
    this->returnBtn->setText(QString::fromUtf8("返回"));

    this->ensureBtn=new QPushButton();
    this->ensureBtn->setText(QString::fromUtf8("确认"));

    this->user_idLab=new QLabel();
    this->user_idLab->setText(QString::fromUtf8("操作者id："));
    this->tings_nameLab=new QLabel();
    this->tings_nameLab->setText(QString::fromUtf8("物品名称："));
    this->tings_jurLab=new QLabel();
    this->tings_jurLab->setText(QString::fromUtf8("物品类型："));
    this->tings_moneyLab=new QLabel();
    this->tings_moneyLab->setText(QString::fromUtf8("物品价格："));
    this->tings_countLab=new QLabel();
    this->tings_countLab->setText(QString::fromUtf8("物品数量："));

    this->user_idEdit=new QLineEdit();
    this->tings_nameEdit=new QLineEdit();
    this->tings_jurEdit=new QLineEdit();
    this->tings_moneyEdit=new QLineEdit();
    this->tings_countEdit=new QLineEdit();

    this->LabLay=new QVBoxLayout();
    this->LabLay->addWidget(this->user_idLab);
    this->LabLay->addWidget(this->tings_nameLab);
    this->LabLay->addWidget(this->tings_jurLab);
    this->LabLay->addWidget(this->tings_moneyLab);
    this->LabLay->addWidget(this->tings_countLab);

    this->EditLay=new QVBoxLayout();
    this->EditLay->addWidget(this->user_idEdit);
    this->EditLay->addWidget(this->tings_nameEdit);
    this->EditLay->addWidget(this->tings_jurEdit);
    this->EditLay->addWidget(this->tings_moneyEdit);
    this->EditLay->addWidget(this->tings_countEdit);

    this->L_eLay=new QHBoxLayout();
    this->L_eLay->addLayout(this->LabLay);
    this->L_eLay->addLayout(this->EditLay);

    this->BtnLay=new QHBoxLayout();
    this->BtnLay->addWidget(this->returnBtn);
    this->BtnLay->addWidget(this->ensureBtn);

    this->Lay=new QVBoxLayout();
    this->Lay->addLayout(this->L_eLay);
    this->Lay->addLayout(this->BtnLay);

    this->setLayout(this->Lay);


}

void InWidget::init_Widget()
{
    this->setWindowTitle(QString::fromUtf8("用户界面"));
    this->resize(800,600);
}

void InWidget::init_Connect()
{
    connect(this->ensureBtn,SIGNAL(clicked()),this,SLOT(on_ensureBtn_cliked()));
    connect(this->returnBtn,SIGNAL(clicked()),this,SLOT(on_retutrnBtn_cliked()));
}

void InWidget::doStockIn()
{
    int FindTure,AddTure;
    QString adduser_id=this->user_idEdit->text();
    QString addthingsname=this->tings_nameEdit->text();
    QString addthingsjur=this->tings_jurEdit->text();
    QString addtingsmoney=this->tings_moneyEdit->text();
    QString addtingscount=this->tings_countEdit->text();
    this->model=this->control->getInstance()->Creatmodel("wuping");
    QString sql=QString("SELECT *FROM StockRecord WHERE tings_name='%1';").arg(addthingsname);

    if(adduser_id==nullptr||addthingsname==nullptr||addthingsjur==nullptr||addtingsmoney==nullptr||addtingscount==nullptr)
    {
         QMessageBox::critical(this, "错误", "输入为空！！");
         return;
    }
    //查找物品名字
    FindTure=this->model->Table_Query((char*)sql.toStdString().c_str());
    if(FindTure>=1)
    {
      //如果物品名存在则加数量
        this->user_idEdit->clear();

        this->tings_nameEdit->clear();

        this->tings_jurEdit->clear();

        this->tings_moneyEdit->clear();

        this->tings_countEdit->clear();
        //对物品表插入数据
        sql=QString("UPDATE StockRecord SET tings_count=tings_count+%1 WHERE tings_name='%2';").arg(addtingscount).arg(addthingsname);
        AddTure=this->model->Table_Inserter((char*)sql.toStdString().c_str());
        if(AddTure)
        {
            QMessageBox::information(this,"成功","添加成功");

            sql=QString("SELECT *FROM StockRecord WHERE tings_name='%1';").arg(addthingsname);

            this->addstock=this->model->getStockByName((char*)sql.toStdString().c_str());//将员工数据从数据库拉出来

            this->model=this->control->getInstance()->Creatmodel("inout");
            // qDebug()<<adduser_id<<" "<<addstock[0].getTings_id()<<" "<<addtingscount;
            sql=QString("INSERT INTO InOut_Stock_Table(user_id,tings_id,do_type,number) VALUES (%1,%2,0,%3);").arg(adduser_id).arg(addstock.getTings_id()).arg(addtingscount);
            AddTure=this->model->Table_Inserter((char*)sql.toStdString().c_str());
            qDebug()<<"AddTure:"<<AddTure;
            emit returnManager();
        }
        else
        {
         QMessageBox::critical(this, "错误", "添加错误");
        }
        emit returnManager();
    }
    else
    {
        //物品名不存在则加数据
        qDebug()<<"没找到物品名";
     sql=QString("INSERT INTO StockRecord(user_id,tings_name,tings_jur,tings_money,tings_count)VALUES (%1,'%2','%3',%4,%5)").arg(adduser_id).arg(addthingsname).arg(addthingsjur).arg(addtingsmoney).arg(addtingscount);
    AddTure=this->model->Table_Inserter((char*)sql.toStdString().c_str());
    if(AddTure)
    {
        QMessageBox::information(this,"成功","添加成功");
        //清除文本框文字
        this->user_idEdit->clear();

        this->tings_nameEdit->clear();

        this->tings_jurEdit->clear();

        this->tings_moneyEdit->clear();

        this->tings_countEdit->clear();

        //对出入库操作表插入入库操作
        sql=QString("SELECT *FROM StockRecord WHERE tings_name='%1';").arg(addthingsname);

        this->addstock=this->model->getStockByName((char*)sql.toStdString().c_str());//将员工数据从数据库拉出来

        this->model=this->control->getInstance()->Creatmodel("inout");

        sql=QString("INSERT INTO InOut_Stock_Table(user_id,tings_id,do_type,number) VALUES (%1,%2,0,%3);").arg(adduser_id).arg(addstock.getTings_id()).arg(addtingscount);
        AddTure=this->model->Table_Inserter((char*)sql.toStdString().c_str());
        qDebug()<<"AddTure:"<<AddTure;
        emit returnManager();
        emit returnManager();
    }
    else
    {
     QMessageBox::critical(this, "错误", "添加错误");
    }
    }
}

void InWidget::on_retutrnBtn_cliked()
{
    emit returnManager();
}

void InWidget::on_ensureBtn_cliked()
{
    doStockIn();
}
